package net.crowtheries.wirelessrouting.messages;

import ptolemy.data.IntToken;
import ptolemy.data.RecordToken;
import ptolemy.data.StringToken;
import ptolemy.data.Token;
import ptolemy.kernel.util.IllegalActionException;

public class RREP {
    private RecordToken rrepToken;
    private StringToken SRCIP;
    private StringToken DSTIP;
    private IntToken DSTSEQ;
    private IntToken HOPCOUNT;
    private IntToken LIFE;

    public RREP(String SRCIP, String DSTIP, int DSTSEQ, int HOPCOUNT) throws IllegalActionException{
    	this.SRCIP=new StringToken(SRCIP);
    	this.DSTIP=new StringToken(DSTIP);
        this.DSTSEQ=new IntToken(DSTSEQ);
        this.HOPCOUNT=new IntToken(HOPCOUNT);
        this.LIFE=new IntToken(255);
        updateRREPToken();
    }
    
    public RREP(RecordToken rreq) throws IllegalActionException{ 
        this.DSTIP=(StringToken)rreq.get("DSTIP");
        this.SRCIP=(StringToken)rreq.get("SRCIP");
        this.DSTSEQ=(IntToken)rreq.get("DSTSEQ");
        this.LIFE=(IntToken)rreq.get("LIFE");
        this.HOPCOUNT=(IntToken)rreq.get("HOPCOUNT");
        this.updateRREPToken();
    }

    private void updateRREPToken() throws IllegalActionException{
        rrepToken=new RecordToken(new String[]{
                "SRCIP",
                "DSTIP",
                "DSTSEQ",
                "LIFE",
                "HOPCOUNT"
            },new Token[]{
                 this.SRCIP,
                 this.DSTIP,
                 this.DSTSEQ,
                 this.LIFE,
                 this.HOPCOUNT
            });
    }
    public String getSRCIP(){
        return this.SRCIP.stringValue();
    }

    public String getDSTIP(){
        return this.DSTIP.stringValue();
    }

    public int getDSTSEQ(){
        return this.DSTSEQ.intValue();
    }

    public int getHOPCOUNT(){
        return this.HOPCOUNT.intValue();
    }

    public int getLIFE(){
        return this.LIFE.intValue();
    }
	
    public RecordToken getMessage() throws IllegalActionException{
        return new RecordToken(new String[]{
            "MSGTYPE",
            "MSG"
        },new Token[]{
            new IntToken(2),
            rrepToken
        });
    }

    public RecordToken getRequestToken(String packetSRCIP,String packetDSTIP) throws IllegalActionException{
        RecordToken message=new RecordToken(new String[]{
            "MSGTYPE",
            "MSG"
        },new Token[]{
            new IntToken(2),
            rrepToken
        });

        RecordToken segUDP=new RecordToken(new String[]{
            "DSTP",
            "SRCP",
            "DATA"
        },new Token[]{
            new IntToken(654),
            new IntToken(654),
            message
        });

        RecordToken packet=new RecordToken(new String[]{
        	"SRCIP",
            "DSTIP",
            "SEG"
        },new Token[]{
        	new StringToken(packetSRCIP),
            new StringToken(packetDSTIP),
            segUDP
        });
        return packet;
    }

}
